package com.tjshop.android.adapter;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.tjshop.android.GoodsListActivity;
import com.tjshop.android.R;
import com.tjshop.android.model.tj.json.Category;
import com.tjshop.android.util.ImageLoaderUtils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryListAdapter extends BaseAdapter {

	List<Object> objs = new ArrayList<Object>();
	private LayoutInflater mInflater;
	private List<Category> categorys;
	private Context mContext;

	public CategoryListAdapter(Context context, List<Category> homeData) {
		mInflater = ((Activity) context).getLayoutInflater();
		this.categorys = homeData;
		this.mContext = context;
		for (int i = 0; i < homeData.size(); i++) {
			objs.add(homeData.get(i));
			for (int j = 0; j < homeData.get(i).getSublist().size();) {
				int remain = homeData.get(i).getSublist().size() - j;
				if (remain >= 4) {
					List<Object> items = new ArrayList<Object>();
					for (int m = j; m < j + 4; m++) {
						items.add(homeData.get(i).getSublist().get(m));
					}
					objs.add(items);
					j += 4;
				} else {
					List<Object> items = new ArrayList<Object>();
					for (int m = j; m < j + remain; m++) {
						items.add(homeData.get(i).getSublist().get(m));
					}
					objs.add(items);
					j += remain;
				}
			}
		}
	}

	@Override
	public int getCount() {
		int count = 0;
		for (int i = 0; i < categorys.size(); i++) {
			count += categorys.get(i).getSublist().size() / 4
					+ (categorys.get(i).getSublist().size() % 4 == 0 ? 0 : 1);
		}
		return count + categorys.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_sub_category, null);
			holder = new ViewHolder();

			holder.icon = (ImageView) convertView.findViewById(R.id.cat_icon);
			holder.name = (TextView) convertView.findViewById(R.id.cat_name);
			holder.tv1 = (TextView) convertView.findViewById(R.id.tv1);
			holder.tv1.setOnClickListener(listener);
			holder.tv2 = (TextView) convertView.findViewById(R.id.tv2);
			holder.tv2.setOnClickListener(listener);
			holder.tv3 = (TextView) convertView.findViewById(R.id.tv3);
			holder.tv3.setOnClickListener(listener);
			holder.tv4 = (TextView) convertView.findViewById(R.id.tv4);
			holder.tv4.setOnClickListener(listener);
			holder.header = (LinearLayout) convertView
					.findViewById(R.id.header);
			holder.item = (LinearLayout) convertView.findViewById(R.id.item);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			LayoutParams params = (LayoutParams) holder.header
					.getLayoutParams();
			if (params == null) {
				params = new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.MATCH_PARENT);
			}
			params.topMargin = 0;
			holder.header.setLayoutParams(params);
		}
		if (isHeader(position)) {
			holder.item.setVisibility(View.GONE);
			holder.header.setVisibility(View.VISIBLE);
			holder.name.setText(getHeaderTitle(position));
			String iconPath = getIconPath(position);
			if (iconPath != null && !iconPath.equals("")) {
				ImageLoaderUtils.getInstance(mContext).displayImage(iconPath,
						holder.icon);
			}
		} else {
			holder.item.setVisibility(View.VISIBLE);
			holder.header.setVisibility(View.GONE);

			TextView[] t = new TextView[] { holder.tv1, holder.tv2, holder.tv3,
					holder.tv4 };
			List<com.tjshop.android.model.tj.json.SubCatagory> subs = getSubs(position);
			for (int i = 0, j = 0; j < 4 & i < subs.size(); j++, i++) {
				t[j].setText(subs.get(i).getName());
				t[j].setVisibility(View.VISIBLE);
				t[j].setTag(subs.get(i));
			}
		}
		return convertView;
	}

	private List<com.tjshop.android.model.tj.json.SubCatagory> getSubs(
			int position) {
		return (List<com.tjshop.android.model.tj.json.SubCatagory>) objs
				.get(position);
	}

	private String getIconPath(int position) {
		Category c = (Category) objs.get(position);
		return c.getIcon();
	}

	private CharSequence getHeaderTitle(int position) {
		Category c = (Category) objs.get(position);
		return c.getName();
	}

	private boolean isHeader(int position) {
		return objs.get(position) instanceof Category;
	}

	OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			if (arg0 instanceof TextView) {
				com.tjshop.android.model.tj.json.SubCatagory sc = (com.tjshop.android.model.tj.json.SubCatagory) arg0
						.getTag();
				Bundle b = new Bundle();
				Toast.makeText(mContext, sc.getName(), Toast.LENGTH_SHORT)
						.show();
				b.putString("gId", sc.getSecond_id());
				b.putString("name", sc.getName());
				b.putString("pid", sc.getFirst_id());
				Intent intent = new Intent();
				intent.putExtras(b);
				intent.setClass(mContext, GoodsListActivity.class);
				mContext.startActivity(intent);
			}
		}
	};

	class ViewHolder {
		ImageView icon;
		TextView name;
		TextView tv1;
		TextView tv2;
		TextView tv3;
		TextView tv4;
		LinearLayout header;
		LinearLayout item;
	}
}
